package com.xuqm.sdhbwfu.app.ui

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.xuqm.base.adapter.CommonAdapter
import com.xuqm.base.adapter.ViewHolder
import com.xuqm.base.dialog.loading.LoadingDialog
import com.xuqm.base.extensions.showMessage
import com.xuqm.base.ui.BaseActivity
import com.xuqm.sdhbwfu.app.R
import com.xuqm.sdhbwfu.app.databinding.ActivityEnterYlDetailBinding
import com.xuqm.sdhbwfu.app.model.InspectionListModel
import com.xuqm.sdhbwfu.app.model.YlListModel
import com.xuqm.sdhbwfu.app.viewmodel.InspectionViewModel

/***
 * 演练详情
 * */
class YLEnterDetailActivity : BaseActivity<ActivityEnterYlDetailBinding>() {
    override fun getLayoutId(): Int =
        R.layout.activity_enter_yl_detail

    override fun fullscreen(): Boolean = true

    private val vm: InspectionViewModel by viewModels()
    private val mData = mutableListOf<InspectionListModel>()
    private val mData1 = mutableListOf<InspectionListModel>()
    private val mData2 = mutableListOf<InspectionListModel>()

    private var current = 1

    override fun initData() {
        super.initData()
        val item = intent.getParcelableExtra<YlListModel>("data")
        binding.ylDetailTitle.text = item.no
        binding.ylDetailDesc.text = item.desc

        vm.list.observe(this) {
            LoadingDialog.dismissDialog()
            mData1.clear()
            if (!it.isNullOrEmpty())
                mData1.addAll(it)
            notifyDataSetChanged()
        }
        vm.list1.observe(this) {
            LoadingDialog.dismissDialog()
            mData2.clear()
            if (it.isNotEmpty())
                mData2.addAll(it)
            notifyDataSetChanged()
        }
        vm.getStatus.observe(this) {

            LoadingDialog.dismissDialog()
            if (!it.isNullOrEmpty()) {
                "领取成功".showMessage()
            }
            mData1.clear()
            mData2.clear()
            vm.inspectionList()
            vm.inspectionListing()
        }
        mData.addAll(mData2)
    }

    override fun onResume() {
        super.onResume()
        LoadingDialog.showDialog("")
        vm.inspectionList()
        vm.inspectionListing()
    }

    private fun notifyDataSetChanged() {
        when (current) {
            1 -> {
                mData.clear()
                mData.addAll(mData2)
                adapter.notifyDataSetChanged()
            }
            2 -> {
                mData.clear()
                mData.addAll(mData1)
                adapter.notifyDataSetChanged()
            }
        }

    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        binding.inspectionBackLyt.setOnClickListener {
            finish()
        }
        binding.inspectionTodoTv.setOnClickListener {
            current = 1
            notifyDataSetChanged()
            binding.inspectionTodoTv.setTextColor(Color.parseColor("#2194D3"))
            binding.inspectionPullTv.setTextColor(Color.parseColor("#555354"))

            binding.inspectionTodoLine.setBackgroundColor(Color.parseColor("#2194D3"))
            binding.inspectionPullLine.setBackgroundColor(Color.parseColor("#d8d8d8"))
        }
        binding.inspectionPullTv.setOnClickListener {
            current = 2
            notifyDataSetChanged()
            binding.inspectionPullTv.setTextColor(Color.parseColor("#2194D3"))
            binding.inspectionTodoTv.setTextColor(Color.parseColor("#555354"))

            binding.inspectionPullLine.setBackgroundColor(Color.parseColor("#2194D3"))
            binding.inspectionTodoLine.setBackgroundColor(Color.parseColor("#d8d8d8"))
        }
        binding.inspectionListRv.layoutManager = GridLayoutManager(mContext, 3)
        binding.inspectionListRv.adapter = adapter
    }

    private val adapter =
        object : CommonAdapter<InspectionListModel>(R.layout.item_inspecion_list, mData) {
            override fun convert(holder: ViewHolder, item: InspectionListModel, position: Int) {

                holder.setText(R.id.inspection_title, item.name)
                holder.setText(R.id.inspection_time, "作业时间:${item.startTaskTime}")

                when (current) {
                    2 -> {
                        holder.visible(R.id.inspection_get_tv)
                        holder.gone(R.id.inspection_confirm_tv)
                    }
                    else -> {
                        holder.visible(R.id.inspection_confirm_tv)
                        holder.gone(R.id.inspection_get_tv)
                    }
                }

                holder.setClickListener(R.id.inspection_confirm_tv) {
                    startActivity(Intent(mContext, ProcessingActivity::class.java)
                        .apply {
                            putExtra("id", item.id)
                        }
                    )
                }
                holder.setClickListener(R.id.inspection_get_tv) {
                    LoadingDialog.showDialog("")
                    vm.inspectionGet(item.id)
                }

            }

        }

}